function mongodbModel(dbname, dataform) {
    var MongoClient;
    var DB_CONN_STR;
    this.init = function () {
        MongoClient = require('mongodb').MongoClient;
        DB_CONN_STR = 'mongodb://localhost:27017/' + dbname;
    };
    /*这里是插入数据*/
    this.insert = function (data) {
        return new Promise((resolve, reject) => {
            MongoClient.connect(DB_CONN_STR, function (err, db) {
                console.log('连接成功')
                var collection = db.db(dbname).collection(dataform);
                collection.insert(data, function (err, result) {
                    resolve(result);
                })
            })
        });
    }
    /*这里是删除数据*/
    this.remove = function (data) {
        return new Promise((resolve, reject) => {
            MongoClient.connect(DB_CONN_STR, function (err, db) {
                console.log('连接成功')
                var collection = db.db(dbname).collection(dataform);
                collection.remove(data, function (err, result) {
                    resolve(result);
                })
            })
        });
    }
    /*这里是修改*/
    this.update = function (data, updata) {
        return new Promise((resolve, reject) => {
            MongoClient.connect(DB_CONN_STR, function (err, db) {
                console.log('连接成功')
                var collection = db.db(dbname).collection(dataform);
                collection.update(data, updata, function (err, data) {
                    resolve(data);
                })
            })
        });
    }
    this.find = function (data, config = null) {
        return new Promise((resolve, reject) => {
            MongoClient.connect(DB_CONN_STR, function (err, db) {
                var cache = db.db(dbname).collection(dataform).find(data)
                if (config) {
                    if (config.sortBy && config.order) {
                        var sor = JSON.parse("{\"" + config.sortBy + "\":" + config.order + "}")
                        cache = cache.sort(sor)
                    }
                    if (config.page && config.size) {
                        var limit = config.size;
                        var skip = (config.page - 1) * config.size
                        cache = cache.skip(skip).limit(limit)
                    }
                }
                cache.toArray(function (err, data) {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(data)
                    }
                });
            })
        })
    }
}

module.exports = mongodbModel;